/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.domain;

import static at.irian.demo.jsfatwork.domain.DomainQueryDetails.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Gerhard Petracek
 */
@Entity
@Table(name = "T_Group")
@NamedQueries({
        @NamedQuery(name = FIND_SUBSCRIBED_GROUPS,
                query = "select g from Group g join g.users su where su = " + C_PARAMETER_USER),
        @NamedQuery(name = FIND_UNSUBSCRIBED_GROUPS,
                query = "select g from Group g where " + C_PARAMETER_USER + " not member of g.users")
})
public class Group extends AbstractDomainObject
{
    private static final long serialVersionUID = -3821308345433991346L;

    @Column(nullable = false, unique = true, length = 20)
    private String name;

    @Column(length = 128)
    private String description;

    public void addUser(User user)
    {
        this.users.add(user);
        user.addGroup(this);
    }

    public void removeUser(User user)
    {
        this.users.remove(user);
        user.removeGroup(this);
    }

    /*
    * generated
    */
    @ManyToMany(cascade = CascadeType.PERSIST)
    private Collection<User> users = new ArrayList<User>();

    public Collection<User> getUsers()
    {
        return users;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }
}
